/**
 * JimBot - Java IM Bot
 * Copyright (C) 2006-2009 JimBot project
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ru.jimbot.protocol;

import com.obimp.OBIMP;
import com.obimp.OBIMPConnection;
import com.obimp.Status;
import com.obimp.XStatus;
import com.obimp.listener.ConnectionListener;
import com.obimp.listener.MessageListener;
import com.obimp.listener.UserStatusListener;
import java.util.HashMap;
import ru.jimbot.modules.AbstractProps;
import ru.jimbot.modules.MsgOutQueue;
import ru.jimbot.util.Log;
import ru.jimbot.util.MainProps;

/**
 * Протокол OBIMP
 * @author alex_xpert
 */
public class OBIMPProtocol extends AbstractProtocol implements ConnectionListener, MessageListener, UserStatusListener {
    public AbstractProps  psp;

    public OBIMPProtocol(AbstractProps props){
        psp = props;
        server_1 = MainProps.getStringProperty("obimp.serverDefault");
        port_1 = MainProps.getIntProperty("obimp.portDefault");
        mq = new MsgOutQueue(this, props.getIntProperty("bot.pauseOut"),
                props.getIntProperty("bot.pauseRestart"),
                props.getIntProperty("bot.msgOutLimit"));
    }

    @Override
    public void connect() {
        mq.start();
        con_obimp = new OBIMPConnection(server_1, screenName, password);
        con_obimp.addConnectionListener(this);
        con_obimp.addMessageListener(this);
        con_obimp.addUserStatusListener(this);
        con_obimp.setDebug(false);
        con_obimp.connect();
    }

    @Override
    public void reConnect() {
        disconnect();
        connect();
    }

    @Override
    public void disconnect() {
        mq.stop();
        Log.info("OBIMP ID "+ baseUin + " Offline");            
        con_obimp.disconnect();
        con_obimp.removeConnectionListener(this);
        con_obimp.removeMessageListener(this);
        con_obimp.removeUserStatusListener(this);
        con_obimp = null;
        Log.error("Разрыв соединения: " + baseUin);
        try {
            Log.info("Переподключение через 1 минуту...");
            Thread.sleep(60000);
            this.reConnect();
        } catch (Exception ex) {
            connected = false;
        }
    }

    @Override
    public boolean isOnLine() {
        if(con_obimp == null) return false;
        return connected;
    }

    @Override
    public void sendMsg(String to, String message) {
        try {
            String msg = new String(message.toCharArray());
            OBIMP.sendMsg(con_obimp, to, msg);
        } catch (Exception ex) {
            connected = false;
        }
    }

    public void setStatus(boolean available, String status) {
        OBIMP.setStatus(con_obimp, available ? Status.PRES_STATUS_ONLINE : Status.PRES_STATUS_NOT_AVAILABLE,
                XStatus.NO, available ? "Онлайн" : "Недоступен", status);
    }

    @Override
    public void RemoveContactList(String uin) {
        //Coming soon...
    }

    @Override
    public void addContactList(String uin) {
        //Coming soon...
    }

    @Override
    public boolean isNoAuthUin(String uin){
        return psp.getBooleanProperty("chat.isAuthRequest");
    }

    @Override
    public AbstractProps getProps() {
        return psp;
    }

    @Override
    public void setStatus(int status) {
        OBIMP.setStatus(con_obimp, Status.PRES_STATUS_ONLINE, (byte) status, psp.getStringProperty("icq.STATUS_MESSAGE1"), psp.getStringProperty("icq.STATUS_MESSAGE2"));
    }

    @Override
    public void getMsg(String sendSN, String recivSN, String msg, boolean isOffline) {
        protList.getMsg(sendSN, recivSN, msg, isOffline);
    }

    @Override
    public void getStatus(String sn, int status) {
        protList.getStatus(sn, status);
    }

    @Override
    public boolean userInfoRequest(String sn, String rsn) {
        lastInfo = sn;
        return true;
    }

    @Override
    public void authRequest(String uin, String msg) {
        //Coming soon...
    }

    @Override
    public int getOuteqSize() {return this.mq.size();}

    @Override
    public void onLoginSuccess() {
        connected = true;
        Log.info("OBIMP ID " + baseUin + " Online");            
        setStatus(true, psp.getStringProperty("icq.STATUS_MESSAGE2"));
    }

    @Override
    public void onLoginFailed(String reason) {
        Log.error(baseUin + " - Login Error! Reason: " + reason);
    }

    @Override
    public void onLogout(String reason) {
        Log.error(baseUin + " - Logout! Reason: " + reason);
    }

    @Override
    public void onIncomingMessage(String id, String text) {
        if(MainProps.isIgnor(id)){
            Log.flood2("IGNORE LIST: " + id + "->" + screenName + ": " + text);
            return;
        }
        protList.getMsg(id, screenName, text,false);
    }

    @Override
    public void onUserOffline(String id) {
        //Coming soon...
    }

    public void onUserOnline(HashMap user) {
        //Coming soon...
    }

}
